/* 
 * GrayRabbit Cms基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.content.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.constant.DictionaryStaticValues;
import org.grayrabbit.cms.dao.content.ContentDao;
import org.grayrabbit.cms.dao.content.LinkContentDao;
import org.grayrabbit.cms.entity.LinkContent;
import org.grayrabbit.cms.vo.content.ContentBean;
import org.grayrabbit.cms.vo.content.LinkContentBean;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>LinkContent</b> dao implementation 

 * @version 1.0，创建时间：2011-05-16 14:40:11 
 */
@Repository
public class LinkContentDaoImpl extends GenericHibernateDao<LinkContent, String>
		implements LinkContentDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From LinkContent as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see ContentDao#assembly(ContentBean)
	 */
	public QueryCondition assembly(LinkContentBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see ContentDao#assemblyCount(ContentBean)
	 */
	public QueryCondition assemblyCount(LinkContentBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * @see ContentDao#count(ContentBean)
	 */
	public int count(LinkContentBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see ContentDao#assemblyNameQuery(ContentBean)
	 */
	public QueryCondition assermblyNameQuery(LinkContentBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see ContentDao#assemblyNameQueryCount(ContentBean)
	 */
	public QueryCondition assemblyNameQueryCount(LinkContentBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see ContentDao#findByPage(Page, ContentBean)
	 */
	public List<LinkContent> findByPage(Page<LinkContent> page,
			LinkContentBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		return findByQc(qc);
	}
	// 业务查询主要逻辑代码
	private void transNameQuery(LinkContentBean bean,QueryCondition qc){
		//TODO
		if(bean!=null){
			if(bean.getContentTitle() != null){
				qc.add("And obj.contentTitle like ? ", bean.getContentTitle()+"%");
			}
            if(bean.getKeywords() != null){
				qc.add("And obj.keywords like ? ", bean.getKeywords()+"%");
			}
            if(bean.getContentContext() != null){
				qc.add("And obj.contentContext like ? ", bean.getContentContext()+"%");
			}
            if(bean.getContentDescription() != null){
				qc.add("And obj.contentDescription like ? ", bean.getContentDescription()+"%");
			}
            if(bean.getContentAuthor() != null){
				qc.add("And obj.contentAuthor like ? ", bean.getContentAuthor()+"%");
			}
            if(bean.getCreateTime() != null){
				qc.add("And obj.createTime like ? ", bean.getCreateTime());
			}
            if(bean.getCreateLoginName() != null){
				qc.add("And obj.createLoginName like ? ", bean.getCreateLoginName()+"%");
			}
            if(bean.getCreateRealName() != null){
				qc.add("And obj.createRealName like ? ", bean.getCreateRealName()+"%");
			}
            if(bean.getUpdateLoginName() != null){
				qc.add("And obj.updateLoginName like ? ", bean.getUpdateLoginName()+"%");
			}
            if(bean.getUpdateRealName() != null){
				qc.add("And obj.updateRealName like ? ", bean.getUpdateRealName()+"%");
			}
            if(bean.getUpdateTime() != null){
				qc.add("And obj.updateTime like ? ", bean.getUpdateTime());
			}
            if(bean.getAuditLoginName() != null){
				qc.add("And obj.auditLoginName like ? ", bean.getAuditLoginName()+"%");
			}
            if(bean.getAuditRealName() != null){
				qc.add("And obj.auditRealName like ? ", bean.getAuditRealName()+"%");
			}
            if(bean.getAuditTime() != null){
				qc.add("And obj.auditTime like ? ", bean.getAuditTime());
			}
            if(bean.getPublishLoginName() != null){
				qc.add("And obj.publishLoginName like ? ", bean.getPublishLoginName()+"%");
			}
            if(bean.getPublishRealName() != null){
				qc.add("And obj.publishRealName like ? ", bean.getPublishRealName()+"%");
			}
            if(bean.getPublishTime() != null){
				qc.add("And obj.publishTime like ? ", bean.getPublishTime());
			}
            if(bean.getContentStatus() != null){
				qc.add("And obj.contentStatus like ? ", bean.getContentStatus()+"%");
			}
            if(bean.getContentHits() != null){
				qc.add("And obj.contentHits like ? ", bean.getContentHits()+"%");
			}
            if(bean.getContentTpl() != null){
				qc.add("And obj.contentTpl like ? ", bean.getContentTpl()+"%");
			}
            if(bean.getIsTop() != null){
				qc.add("And obj.isTop like ? ", bean.getIsTop());
			}
            if(bean.getContentSource() != null){
				qc.add("And obj.contentSource like ? ", bean.getContentSource()+"%");
            }
            if(bean.getCategory()!=null&&StringUtils.isNotBlank(bean.getCategory().getCategoryId())){
            	qc.add("And obj.category.categoryId like ? ", bean.getCategory().getCategoryId());
            }
		}
	}

	public List<LinkContent> findContent(String categoryId,int max) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(StringUtils.isNotBlank(categoryId))
			qc.add(" And  obj.category.categoryId = ? " ,categoryId);
		qc.add("And ( obj.contentStatus = ? ", DictionaryStaticValues.CONTENT_STATUS_PUBLISH);
		qc.add(" or obj.contentStatus = ? ) ", DictionaryStaticValues.CONTENT_STATUS_SUCCESS);
		qc.appenOrderBy(" obj.ord asc,obj.createTime desc,obj.publishTime desc");
		if(max > 0) qc.setMaxResults(max);
		return findByQc(qc);
	}
}